//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.runjian.demo.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.net.InetSocketAddress;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class CommonUtil {
    private static final Logger log = LoggerFactory.getLogger(CommonUtil.class);
    private static final String UNKNOW = "unknown";
    public static final String NULL = "NULL";
    public static final String UNDEFINED = "UNDEFINED";

    public CommonUtil() {
    }

    public static String camelToUnderscore(String value) {
        if (StrUtil.isEmpty(value)) {
            return value;
        } else {
            String[] arr = org.apache.commons.lang3.StringUtils.splitByCharacterTypeCamelCase(value);
            if (arr.length == 0) {
                return value;
            } else {
                StringBuilder result = new StringBuilder();
                IntStream.range(0, arr.length).forEach((i) -> {
                    if (i != arr.length - 1) {
                        result.append(arr[i]).append("_");
                    } else {
                        result.append(arr[i]);
                    }

                });
                return org.apache.commons.lang3.StringUtils.lowerCase(result.toString());
            }
        }
    }

    public static String underscoreToCamel(String value) {
        StringBuilder result = new StringBuilder();
        String[] arr = value.split("_");
        String[] var3 = arr;
        int var4 = arr.length;

        for (int var5 = 0; var5 < var4; ++var5) {
            String s = var3[var5];
            result.append(String.valueOf(s.charAt(0)).toUpperCase()).append(s.substring(1));
        }

        return result.toString();
    }

    public static boolean isAjaxRequest(HttpServletRequest request) {
        return request.getHeader("X-Requested-With") != null && "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
    }

    public static boolean match(String regex, String value) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }

    public static HttpServletRequest getHttpServletRequest() {
        try {
            RequestAttributes request = RequestContextHolder.getRequestAttributes();
            return Objects.isNull(request) ? null : ((ServletRequestAttributes) request).getRequest();
        } catch (Exception var1) {
            log.error("HttpServletRequest 获取为空!");
            return null;
        }
    }

    public static String getHttpServletRequestIpAddress() {
        HttpServletRequest request = getHttpServletRequest();
        return Objects.isNull(request) ? "" : getHttpServletRequestIpAddress(request);
    }

    public static String getHttpServletRequestIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip) && ip.contains(",")) {
            ip = ip.split(",")[0];
            if (ip.contains(":")) {
                ip = ip.split(":")[0];
            }
        }

        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }

    public static String getServerHttpRequestIpAddress(ServerHttpRequest request) {
        HttpHeaders headers = request.getHeaders();
        String ip = headers.getFirst("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip) && ip.contains(",")) {
            ip = ip.split(",")[0];
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("HTTP_CLIENT_IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("HTTP_X_FORWARDED_FOR");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = headers.getFirst("X-Real-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = ((InetSocketAddress) Objects.requireNonNull(request.getRemoteAddress())).getAddress().getHostAddress();
        }

        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
    }

    public static boolean equals(Object cs1, Object cs2) {
        return Objects.equals(cs1, cs2);
    }

    public static <T> boolean isEmpty(List<T> list) {
        return CollUtil.isEmpty(list);
    }

    public static <T> boolean isNotEmpty(List<T> list) {
        return CollUtil.isNotEmpty(list);
    }

    public static <T, T2> boolean isEmpty(Map<T, T2> list) {
        return CollUtil.isEmpty(list);
    }

    public static <T, T2> boolean isNotEmpty(Map<T, T2> list) {
        return CollUtil.isNotEmpty(list);
    }

    public static <T> boolean isBlank(String obj) {
        return org.apache.commons.lang3.StringUtils.isBlank(obj);
    }

    public static <T> boolean isNotBlank(String obj) {
        return org.apache.commons.lang3.StringUtils.isNotBlank(obj);
    }

    public static <T> boolean isNull(T obj) {
        return Objects.isNull(obj);
    }

    public static <T> boolean nonNull(T obj) {
        return Objects.nonNull(obj);
    }

    public static String defaultIfBlank(String obj, String defaults) {
        return StrUtil.isNotBlank(obj) ? obj : defaults;
    }

    public static <T> T defaultIfNull(T obj, T defaults) {
        return Objects.nonNull(obj) ? obj : defaults;
    }

    public static <T> List<T> defaultIfEmpty(List<T> list, List<T> defaults) {
        return CollUtil.isNotEmpty(list) ? list : defaults;
    }

    public static <T, T2> Map<T, T2> defaultIfEmpty(Map<T, T2> map, Map<T, T2> defaults) {
        return CollUtil.isNotEmpty(map) ? map : defaults;
    }

    public static List<String> toStrList(String strArr) {
        return toStrList(strArr, ",");
    }

    public static List<String> toStrList(String strArr, String comma) {
        if (StrUtil.isBlank(strArr)) {
            return new ArrayList();
        }
        String[] split = strArr.split(comma);
        if (Objects.isNull(split)) {
            return new ArrayList<>();
        }
        return Arrays.stream(split).filter((p) -> {
            return StrUtil.isNotBlank(p) && !Objects.equals("NULL", p.toUpperCase()) && !Objects.equals("UNDEFINED", p.toUpperCase());
        }).collect(Collectors.toList());
    }

    public static List<Integer> toIntList(String strArr) {
        return toIntList(strArr, ",");
    }

    public static List<Integer> toIntList(String strArr, String comma) {
        if (StrUtil.isBlank(strArr)) {
            return new ArrayList();
        } else {
            String[] split = strArr.split(comma);
            return (List) (Objects.isNull(split) ? new ArrayList() : (List) Arrays.stream(split).filter((p) -> {
                return StrUtil.isNotBlank(p) && !Objects.equals("NULL", p.toUpperCase()) && !Objects.equals("UNDEFINED", p.toUpperCase());
            }).map((p) -> {
                return Integer.valueOf(p);
            }).collect(Collectors.toList()));
        }
    }

    public static List<Long> toLongList(String strArr) {
        return toLongList(strArr, ",");
    }

    public static List<Long> toLongList(String strArr, String comma) {
        if (StrUtil.isBlank(strArr)) {
            return new ArrayList();
        } else {
            String[] split = strArr.split(comma);
            return (List) (Objects.isNull(split) ? new ArrayList() : (List) Arrays.stream(split).filter((p) -> {
                return StrUtil.isNotBlank(p) && !Objects.equals("NULL", p.toUpperCase()) && !Objects.equals("UNDEFINED", p.toUpperCase());
            }).map((p) -> {
                return Long.valueOf(p);
            }).collect(Collectors.toList()));
        }
    }

    public static Integer[] strArrToIntArr(String[] ids) {
        Integer[] ints = new Integer[ids.length];

        for (int i = 0; i < ids.length; ++i) {
            String id = ids[i];
            if (!Objects.equals("NULL", id.toUpperCase()) && !Objects.equals("UNDEFINED", id.toUpperCase())) {
                ints[i] = Integer.parseInt(ids[i]);
            }
        }

        return ints;
    }

    public static Long[] strArrToLongArr(String[] ids) {
        Long[] longs = new Long[ids.length];

        for (int i = 0; i < ids.length; ++i) {
            String id = ids[i];
            if (!Objects.equals("NULL", id.toUpperCase()) && !Objects.equals("UNDEFINED", id.toUpperCase())) {
                longs[i] = Long.parseLong(id);
            }
        }

        return longs;
    }

}
